package com.supcalls.atomic;

import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            int expectedValue = atomicInteger.get();
            boolean success = atomicInteger.compareAndSet(expectedValue, expectedValue + 1);
            System.out.println("Thread-1 CAS result: " + success);
        });

        Thread t2 = new Thread(() -> {
            int expectedValue = atomicInteger.get();
            boolean success = atomicInteger.compareAndSet(expectedValue, expectedValue + 1);
            System.out.println("Thread-2 CAS result: " + success);
        });

        t1.start();
        t2.start();
    }
}
